package org.hbrs.ooka.model;

import java.util.ArrayList;
import java.util.List;

import org.hbrs.ooka.model.persistence.CustomerDAO;
import org.hbrs.ooka.model.persistence.ProductDAO;

public class Customer {

	private int m_id = -1;
	private String m_name = "";
	private String m_vorname = "";
	private String m_ort = "";

	private CustomerDAO m_customerDAO = null;
	private ProductDAO m_productDAO = null;

	private List<Integer> m_productsIDs = null;

	public Customer(String vname, String nname, String loc, CustomerDAO cdao,
			ProductDAO pdao) {
		this.m_name = nname;
		this.m_vorname = vname;
		this.m_ort = loc;
		this.m_id = this.hashCode();
		this.m_customerDAO = cdao;
		this.m_productDAO = pdao;
		this.m_productsIDs = new ArrayList<>();
	}

	@Override
	public String toString() {
		return this.m_vorname + " " + this.m_name + " aus " + this.m_ort
				+ " kauft " + this.getProducts() + ".";
	}

	public void moveHome(String newLoc) {
		this.m_ort = newLoc;
		this.m_customerDAO.updateCustomer(this);
	}

	public void rename(String newName) {
		this.m_name = newName;
		this.m_customerDAO.updateCustomer(this);
	}

	public int getId() {
		return this.m_id;
	}
	
	public void setId(int id) {
		this.m_id = id;
	}

	public String getName() {
		return this.m_name;
	}
	
	public String getOrt() {
		return this.m_ort;
	}

	public String getVName() {
		return this.m_vorname;
	}

	public void addProduct(Product p) {
		int id = this.m_productDAO.insert(p);
		this.m_productsIDs.add(id);
	}

	public List<Product> getProducts() {
		List<Product> result = new ArrayList<>();
		for (Integer id : this.m_productsIDs) {
			result.add(this.m_productDAO.findByID(id));
		}

		return result;
	}

	public void removeProduct(Product p) {
		System.out.println("Customer.removeProduct(), remove: " + p);
		this.m_productsIDs.remove(this.m_productsIDs.indexOf(p.getId()));
		this.m_customerDAO.removeProduct(this, p.getId());
	}
}
